// 测试SQL正则替换
const sql = `
      SELECT 
        id, username, email, phone, avatar,
        CASE WHEN is_admin = 1 THEN 'admin' ELSE 'user' END as role,
        created_at
      FROM jd_users
      WHERE 1=1
    `;

console.log('原始SQL:');
console.log(sql);

const countSql = sql.replace(/SELECT.*FROM/s, 'SELECT COUNT(*) as total FROM');

console.log('\n替换后的SQL:');
console.log(countSql);

console.log('\n问题：正则表达式是贪婪匹配，会匹配到最后一个FROM！');
console.log('应该使用非贪婪匹配：/SELECT[\\s\\S]*?FROM/');

const correctCountSql = sql.replace(/SELECT[\s\S]*?FROM/, 'SELECT COUNT(*) as total FROM');
console.log('\n正确的替换后SQL:');
console.log(correctCountSql);

